package com.cskaoyan.app.controller;

import com.cskaoyan.app.bean.bo.*;
import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.BaseRespVo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketOrder;
import com.cskaoyan.app.bean.vo.AdminOrderChannelVO;
import com.cskaoyan.app.bean.vo.AdminOrderDetailVO;
import com.cskaoyan.app.exception.ValidationException;
import com.cskaoyan.app.service.AdminOrderService;
import com.cskaoyan.app.util.FileUtil;
import com.cskaoyan.app.util.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author Shuuka
 * @email 1712430536@qq.com
 * @date 2022/12/28 22:33
 */
@RestController
@RequestMapping("admin/order")
public class AdminOrderController {

    @Autowired
    AdminOrderService adminOrderService;

    @GetMapping("list")
    public BaseRespVo<Object> list(BasePageInfo basePageInfo, AdminOrderListBO adminOrderListBO)  {
        CommonData<MarketOrder> data = adminOrderService.list(basePageInfo,adminOrderListBO);
        return BaseRespVo.ok(data);
    }

    @GetMapping("channel")
    public BaseRespVo<Object> channel(){
        List<AdminOrderChannelVO> adminOrderChannelVOS = adminOrderService.channel();
        return BaseRespVo.ok(adminOrderChannelVOS);
    }

    @GetMapping("detail")
    public BaseRespVo<AdminOrderDetailVO> detail(Integer id){
        AdminOrderDetailVO adminOrderDetailVO = adminOrderService.detail(id);
        return BaseRespVo.ok(adminOrderDetailVO);
    }

    @PostMapping("ship")
    public BaseRespVo<Object> ship(@RequestBody AdminOrderShipBO adminOrderShipBO){
        Integer row = adminOrderService.ship(adminOrderShipBO);
        if (row == 1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidParameter("发货失败！");
    }

    @PostMapping("delete")
    public BaseRespVo<Object> delete(@RequestBody @Validated AdminOrderDeleteBO adminOrderDeleteBO) throws ValidationException {
        Integer row = adminOrderService.delete(adminOrderDeleteBO.getOrderId());
        if (row == 1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidParameter("删除失败！");
    }

    @PostMapping("refund")
    public BaseRespVo<Object> refund(@RequestBody @Validated AdminOrderRefundBO adminOrderRefundBO){
        Integer row = adminOrderService.refund(adminOrderRefundBO);
        if (row == 1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidParameter("退款失败！");
    }

    @PostMapping("reply")
    public BaseRespVo<Object> reply(@RequestBody @Validated AdminOrderReplyBO adminOrderReplyBO){
        Integer row = adminOrderService.reply(adminOrderReplyBO);
        if (row == 1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidParameter("回复失败！");
    }


    @PostMapping("upload")
    public BaseRespVo<Object> upload(MultipartFile myFile){
        String url = FileUtil.upLoad(myFile);
        return BaseRespVo.ok(url);
    }
}
